package com.ruoyi.web.controller.business;

import java.io.InputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.business.domain.ProductSku;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.ProductSpu;
import com.ruoyi.business.service.IProductSpuService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 商品SPUController
 *
 * @author ruoyi
 * @date 2025-03-15
 */
@RestController
@RequestMapping("/business/spu")
public class ProductSpuController extends BaseController
{
    @Autowired
    private IProductSpuService productSpuService;

    /**
     * 查询商品SPU列表
     */
    @PreAuthorize("@ss.hasPermi('business:spu:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProductSpu productSpu)
    {
        startPage();
        List<ProductSpu> list = productSpuService.selectProductSpuList(productSpu);
        return getDataTable(list);
    }

    /**
     * 导出商品SPU列表
     */
    @PreAuthorize("@ss.hasPermi('business:spu:export')")
    @Log(title = "商品SPU", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProductSpu productSpu)
    {
        List<ProductSpu> list = productSpuService.selectProductSpuList(productSpu);
        ExcelUtil<ProductSpu> util = new ExcelUtil<ProductSpu>(ProductSpu.class);
        util.exportExcel(response, list, "商品SPU数据");
    }

    /**
     * 获取商品SPU详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:spu:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(productSpuService.selectProductSpuById(id));
    }

    /**
     * 新增商品SPU
     */
    @PreAuthorize("@ss.hasPermi('business:spu:add')")
    @Log(title = "商品SPU", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductSpu productSpu)
    {
        return toAjax(productSpuService.insertProductSpu(productSpu));
    }

    /**
     * 修改商品SPU
     */
    @PreAuthorize("@ss.hasPermi('business:spu:edit')")
    @Log(title = "商品SPU", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductSpu productSpu)
    {
        return toAjax(productSpuService.updateProductSpu(productSpu));
    }

    /**
     * 删除商品SPU
     */
    @PreAuthorize("@ss.hasPermi('business:spu:remove')")
    @Log(title = "商品SPU", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(productSpuService.deleteProductSpuByIds(ids));
    }


    /**
     * 导入商品SPU数据
     */
    @PreAuthorize("@ss.hasPermi('business:spu:import')")
    @Log(title = "商品SPU", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult importData(@RequestParam("file") MultipartFile file)
    {
        if (file.isEmpty()) {
            return AjaxResult.error("文件不能为空！");
        }
            productSpuService.importData(file);
        return AjaxResult.success("导入成功！");
    }
}
